<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="160px" :rules="rules">
      <el-row style="text-align: center">
        <el-col :span="12">
          <el-form-item label="计划类型:" prop="planType">
            <hey-select v-model="formModel.planType" :disabled="disabledEdit" :clearable="false" @change="changeType" clcd="plan_type_cd" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划数量:" prop="qty">
            <el-input v-model="formModel.qty" />
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="showYear">
          <el-form-item label="计划年份:" prop="planYear">
            <el-date-picker
              v-model="formModel.planYear"
              type="year"
              :disabled="disabledEdit"
              value-format="yyyy"
              placeholder="选择年">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="showMonth">
          <el-form-item label="计划月份:" prop="month">
            <el-date-picker
              v-model="formModel.month"
              type="month"
              :disabled="disabledEdit"
              value-format="yyyy-MM"
              placeholder="选择月">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="showPlanDate">
          <el-form-item label="计划日:" prop="planDate">
            <el-date-picker
              v-model="formModel.planDate"
              type="date"
              :disabled="disabledEdit"
              value-format="yyyy-MM-dd"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工厂:" prop="plantNo">
            <el-select v-model="formModel.plantNo" :disabled="disabledPlant" @change="changePlant" clearable filterable>
              <el-option v-for="item in plantOptions" :key="item.tmBasPlantId" :label="item.plantNameC" :value="item.plantNo"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车间:" prop="workshopNo">
            <el-select v-model="formModel.workshopNo" :disabled="disabledWorkshop" @change="changeWorkshop" clearable filterable>
              <el-option v-for="item in workshopOptions" :key="item.tmBasWorkshopId" :label="item.workshopNameC" :value="item.workshopNo"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="showUloc">
          <el-form-item label="交付工位:" prop="ulocNo">
            <el-select v-model="formModel.ulocNo" :disabled="disabledUloc" clearable filterable>
              <el-option v-for="item in ulocOptions" :key="item.tmBasUlocId" :label="item.ulocName" :value="item.ulocNo"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="submit-area">
      <el-col :span="24">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSubmit()">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>

export default {
  name: 'NeedPlanMnt',
  props: ['pageType', 'editData'],
  data() {
    return {
      disabledEdit:false,
      showYear:true,
      showMonth:false,
      showPlanDate:false,
      showUloc:false,
      disabledPlant:false,
      disabledWorkshop:true,
      disabledUloc:true,
      plantOptions:[],
      workshopOptions:[],
      ulocOptions:[],
      formModel: {
        rcrdId: '',
        planType: '01',
        qty: '',
        plantNo: '',
        workshopNo: '',
        ulocNo: '',
        planYear: '',
        month: '',
        planDate: '',
      },
      rules: {
      }
    }
  },
  computed: {

  },
  async mounted() {
    await this.getPlants()
    this.init()
  },
  methods: {
    async changePlant(value){
      if(value){
        this.clearWorkshop()
        this.clearUloc()
        //打开车间
        this.disabledWorkshop = false
        //通过工厂编号查询所有车间
        const param = {pageNum:1,pageSize:9999}
        param.isDelete = '0'
        param.status = '1'
        const plantObj = this.plantOptions.find(item => item.plantNo === value)
        param.tmBasPlantId = plantObj.tmBasPlantId
        await this.get('/tmBasWorkshop/queryTmBasWorkshop', param).then(res => {
          this.workshopOptions = []
          this.workshopOptions = res.list
        })
      }else{
        //清空车间并屏蔽车间
        this.clearWorkshop()
        //清空当前工位并屏蔽当前工位
        this.clearUloc()
      }
    },
    changeWorkshop(value){
      if(value){
        this.clearUloc()
        //打开交付工位
        this.disabledUloc = false
        //通过车间代码查询工位基础信息
        const param = {pageNum:1,pageSize:9999}
        param.isDelete = '0'
        param.status = '1'
        const workshopObj = this.workshopOptions.find(item => item.workshopNo === value)
        param.tmBasWorkshopId = workshopObj.tmBasWorkshopId
        this.get('/tmBasUloc/queryTmBasUloc', param).then(res => {
          this.ulocOptions = []
          this.ulocOptions = res.list
        })
      }else{
        this.clearUloc()
      }
    },
    clearWorkshop(){
      this.workshopOptions = []
      this.disabledWorkshop = true
      this.formModel.workshopNo = ''
    },
    clearUloc(){
      this.ulocOptions = []
      this.disabledUloc = true
      this.formModel.ulocNo = ''
    },
    //获取所有启用的工厂
    async getPlants(){
      const param = {pageNum:1,pageSize:9999}
      param.isDelete = '0'
      param.status = '1'
      await this.get('/tmBasPlant/queryTmBasPlant', param).then(res => {
        this.plantOptions = res.list
      })
    },
    changeType(value){
      if(value=='01'){
        //年计划
        this.formModel.month = ''
        this.formModel.planDate = ''
        this.showYear = true
        this.showMonth = false
        this.showPlanDate = false
        this.showUloc = false
        this.formModel.ulocNo = ''
      }else if(value=='02'){
        //月计划
        this.formModel.planYear = ''
        this.formModel.planDate = ''
        this.showYear = false
        this.showMonth = true
        this.showPlanDate = false
        this.showUloc = false
        this.formModel.ulocNo = ''
      }else if(value=='03'){
        //日计划
        this.formModel.planYear = ''
        this.formModel.month = ''
        this.showYear = false
        this.showMonth = false
        this.showPlanDate = true
        this.showUloc = true
        this.formModel.ulocNo = ''
      }
    },
    async init() {
      this.$form = this.$refs.form
      const {pageType, editData, formModel} = this
      if (pageType !== 'add') {
        this.changeType(editData.planType)
        await this.changePlant(editData.plantNo)
        this.changeWorkshop(editData.workshopNo)
        this.disabledEdit = true
        this.disabledPlant = true
        this.disabledWorkshop = true
        this.disabledUloc = true
        this.assignData(editData, formModel)
      }
      this.initRules()
    },
    initRules() {
      const REQUIRED_MSG = this.CONST.REQUIRED_MSG
      this.rules = {
        planType: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        qty: this.HEY_VALIDATOR({ type: ['MAXINT[8]'],required: true}),
        planYear: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        month: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        planDate: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        plantNo: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        workshopNo: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        ulocNo: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
      }
      this.$nextTick(_ => {
        this.$form.clearValidate()
      })
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const { pageType, formModel } = this
          const param = { ...formModel }
          const operateType = pageType === 'edit' ? '02' : '01'
          param.operateType = operateType
          const url = '/teOfmProPlan/maintainTeOfmProPlan'
          const tip = pageType === 'edit' ? '修改成功！' : '新增成功！'
          this.post(url, param).then(res => {
            this.$info(tip, () => {
              this.$emit('mntSuc', res)
              this.closeDialog()
            })
          })
        }
      })
    },
    closeDialog() {
      this.$parent.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
